package zjitc.controller;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.InternalResourceView;
import zjitc.filter.Login;
import zjitc.po.Book;
import zjitc.po.BookCategory;
import zjitc.po.Evaluate;
import zjitc.po.Modelen;
import zjitc.po.User;
import zjitc.service.BookCategoryService;
import zjitc.service.BookService;
import zjitc.service.EvaluateService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.List;
import zjitc.service.UserService;

/**
 * Created by guo on 2018/5/2.
 */
@Controller
@RequestMapping("/Evaluate")
public class EvaluateController {
  @Autowired
  private Login login;
  @Autowired
  private UserService userService;
    @Autowired
    private EvaluateService evaluateService;
    @Autowired
    private BookService bookService;
    @Autowired
    private BookCategoryService bookCategoryService;
    @RequestMapping("/findEvaluateByBsidAndUid")
    public String findEvaluateByBsidAndUid(HttpServletRequest request, Model model)
    {
       int bsid=Integer.valueOf(request.getParameter("bsid"));
        List<Modelen> elist=evaluateService.findEvaluateByBsidAndUid(bsid);
        Book byBsid = bookService.findByBsid(bsid);
        List<BookCategory> list=bookCategoryService.findAllCategory();
        model.addAttribute("list",list);
        model.addAttribute("book",byBsid);
        model.addAttribute("elist",elist);
        return "bookPage/bookMessage";
    }
    @RequestMapping("/sendToAddEvaluate")
    public String sendToAddEvaluate(Model model,HttpServletRequest request,HttpSession session)
    {
      int bsid = Integer.valueOf(request.getParameter("bsid"));
      String username = (String) session.getAttribute("username");
      User user = userService.findByUsername(username);
      if (username==null)
      {
        model.addAttribute("msg","你还未登录，无法发表评论!");
        return "bookPage/login";
      }
      model.addAttribute("name",user.getName());
      model.addAttribute("bsid",bsid);
      return "bookPage/evaluate";
    }
    @RequestMapping("/addEvaluate")
    public View addEvaluate(HttpServletRequest request, HttpSession session)
    {
     Evaluate evaluate=new Evaluate();
      int bsid = Integer.valueOf(request.getParameter("bsid"));
      String username= (String)session.getAttribute("username");
      User user = userService.findByUsername(username);
      try {
        BeanUtils.populate(evaluate,request.getParameterMap());
        evaluate.setEvaluatetime(new Date());
        evaluate.setBsid(bsid);
       evaluate.setUid(user.getUid());
        int nmu=evaluateService.addEvaluate(evaluate);
      } catch (Exception e) {
        e.printStackTrace();
      }
      View view=new InternalResourceView("/app/Evaluate/findEvaluateByBsidAndUid?bisd="+bsid);
        return view;
    }
    @RequestMapping("/findAllEvaluate")
    public String findAllEvaluate(Model model, HttpSession session, HttpServletRequest request, HttpServletResponse response) throws Exception {
      Object o = session.getAttribute("username");
      login.preHandle(request,response,o);
     List<Evaluate> list=evaluateService.findAllEvaluate();
     model.addAttribute("list",list);
     return "admin/evaluate/list";
    }
}
